import java.util.ArrayList;
import java.util.List;


public class Agent {

	private int type = 0; // make an enum
	private int lastGameType = 0;
	private boolean lastGameWin = true;//(Math.random() > 0.5 ? true : false);
	
	private double cost;
	private double fitness;

	public Agent(int type, double playCost){
		this.type = type;
		this.lastGameType = type;
		this.cost = playCost;
	}

	public void play(ArrayList<Agent> l){
		this.fitness -= this.cost;
		
		int cooperators = 0;
		for(Agent p : l){
			if(p.getLastGame() == 1 && p.getLastGameWin() == true)
				cooperators++;
		}
		int defectors = 0;
		for(Agent p : l){
			if(p.getLastGame() == 0 && p.getLastGameWin() == true)
				defectors++;
		}
		if(defectors > cooperators) {
			this.type = 0;
			cost = Environment.defCost;
		}
		else if (cooperators > defectors) {
			this.type = 1;
			cost = Environment.coopCost;
		}
	}
	
	public void playEx(List<Agent> l) {
		this.fitness -= this.cost;
		
		double max = 0;
		int coolestType = 0;
		for (Agent a : l) {
			if (a.getFitness() > max) {
				max = a.getFitness();
				coolestType = a.getType();
			}
		}
		if (max != 0) {
			type = coolestType;
			
			if (coolestType == 0)
				cost = Environment.defCost;
			else
				cost = Environment.coopCost;
		}
	}

	public int getType(){
		return this.type;
	}
	
	public int getLastGame(){
		return this.lastGameType;
	}
	
	public void setLastGame(int lg){
		this.lastGameType = lg;
	}

	public double getFitness(){
		return this.fitness;
	}
	public void setFitness(double f){
		this.fitness = f;
	}

	public void reward(double amount){
		this.fitness += amount;
	}

	public boolean getLastGameWin() {
		return lastGameWin;
	}

	public void setLastGameWin(boolean lastGameWin) {
		this.lastGameWin = lastGameWin;
	}


}
